package com.zmzncs.lmtc.module.promotion.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmzncs.lmtc.common.pojo.PageInfo;
import com.zmzncs.lmtc.common.pojo.Rest;
import com.zmzncs.lmtc.common.pojo.enums.optionalvalue.promotion.MonthRentTypeEnum;
import com.zmzncs.lmtc.common.pojo.enums.optionalvalue.promotion.RuleStatusEnum;
import com.zmzncs.lmtc.common.util.JsonUtil;
import com.zmzncs.lmtc.common.util.RestUtil;
import com.zmzncs.lmtc.module.park.pojo.park_info.ParkInfoDTO;
import com.zmzncs.lmtc.module.promotion.pojo.month_rule.*;
import com.zmzncs.lmtc.module.promotion.pojo.month_rule_park.MonthRulePark;
import com.zmzncs.lmtc.module.promotion.pojo.month_rule_park.MonthRuleParkVO;
import com.zmzncs.lmtc.module.promotion.pojo.month_rule_fee_scale.MonthRuleFeeScale;
import com.zmzncs.lmtc.module.promotion.service.MonthRuleParkService;
import com.zmzncs.lmtc.module.promotion.service.MonthRuleFeeScaleService;
import com.zmzncs.lmtc.module.promotion.service.MonthRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2020-08-12
 */
@Slf4j
@Api(tags = "月租规则")
@RestController
@RequestMapping("/promotion/month-rule")
@Validated
public class MonthRuleController {

    @Autowired
    MonthRuleService monthRuleService;
    @Autowired
    MonthRuleParkService monthRuleParkService;
    @Autowired
    MonthRuleFeeScaleService monthRuleFeeScaleService;


    @ApiOperation(value = "获取月租类型")
    @ApiImplicitParams({
    })
    @GetMapping("getMonthRuleType")
    public Rest<List<MonthRuleType>> getMonthRuleType() {
        List<MonthRuleType> list = new ArrayList<>();

        MonthRuleType type1 = new MonthRuleType(1,"全天年卡", 365);
        list.add(type1);
        MonthRuleType type2 = new MonthRuleType(2,"全天季卡", 90);
        list.add(type2);
        MonthRuleType type4 = new MonthRuleType(4,"全天月卡", 30);
        list.add(type4);
        MonthRuleType type100 = new MonthRuleType(100,"白天月卡", 30);
        list.add(type100);
        MonthRuleType type110 = new MonthRuleType(110,"夜间月卡", 30);
        list.add(type110);

        return RestUtil.success(list);
    }

    @ApiOperation(value = "添加月租规则")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping(value = "addMonthRule")
    public Rest addMonthRule(@RequestBody @Validated MonthRuleAddForm monthRuleAddForm) {
        monthRuleAddForm.validate();

        monthRuleService.addMonthRule(monthRuleAddForm);

        return RestUtil.success();
    }

    @ApiOperation(value = "修改月租规则")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping(value = "updateMonthRule")
    public Rest updateMonthRule(@RequestBody @Validated MonthRuleUpdateForm monthRuleUpdateForm) throws ExecutionException, InterruptedException {
        monthRuleUpdateForm.validate();

        monthRuleService.updateMonthRule(monthRuleUpdateForm);

        return RestUtil.success();
    }

    @ApiOperation(value = "查询月租规则")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name="pageNum", value="页码", required = true),
            @ApiImplicitParam(paramType="query", name="pageSize", value="页长", required = true),
            @ApiImplicitParam(paramType="query", name="keyword", value="关键字"),
    })
    @GetMapping(value = "getMonthRulePage")
    public Rest<IPage<MonthRuleTableVO>> getMonthRulePage(@NotNull Integer pageNum, @NotNull Integer pageSize, String keyword) {
        IPage<MonthRuleDTO> monthRuleDTOIPage = monthRuleService.getMonthRuleParkList(new Page<>(pageNum, pageSize), keyword);
        PageInfo<MonthRuleTableVO> page = JsonUtil.pageTransform(monthRuleDTOIPage, MonthRuleTableVO.class);

        return RestUtil.success(page);
    }

    @ApiOperation(value = "根据id查询月租规则")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name="id", value="id", required = true),
    })
    @GetMapping(value = "getMonthRuleById")
    public Rest<MonthRuleInfoVO> getMonthRuleById(@NotNull Long id) {
        //  查询时长规则
        MonthRule monthRule = monthRuleService.getById(id);
        //  根据时长规则id查询停车场
        LambdaQueryWrapper<MonthRulePark> monthRuleParkWrapper = new LambdaQueryWrapper();
        monthRuleParkWrapper.eq(MonthRulePark::getMonthRuleId, id);
        List<MonthRulePark> monthRuleParks = monthRuleParkService.list(monthRuleParkWrapper);
        List<MonthRuleParkVO> monthRuleParkVOs = JsonUtil.listTransform(monthRuleParks, MonthRuleParkVO.class);

        //  根据时长规则id查询收费标准
        LambdaQueryWrapper<MonthRuleFeeScale> monthRulePaytypeWrapper = new LambdaQueryWrapper();
        monthRulePaytypeWrapper.eq(MonthRuleFeeScale::getMonthRuleId, id);
        List<MonthRuleFeeScale> monthRuleFeeScales = monthRuleFeeScaleService.list(monthRulePaytypeWrapper);

        //  设置返回类型
        MonthRuleInfoVO monthRuleInfoVO = new MonthRuleInfoVO();
        BeanUtils.copyProperties(monthRule, monthRuleInfoVO);
        monthRuleInfoVO.setMonthRuleParks(monthRuleParkVOs);
        for(MonthRuleFeeScale monthRuleFeeScale : monthRuleFeeScales){
            if(monthRuleFeeScale.getMonthRentType().intValue() == MonthRentTypeEnum.MONTH.getCode()){
                monthRuleInfoVO.setMonthAmount(monthRuleFeeScale.getAmount());
                monthRuleInfoVO.setMonthDays(monthRuleFeeScale.getDays());
            }
            if(monthRuleFeeScale.getMonthRentType().intValue() == MonthRentTypeEnum.QUARTER.getCode()){
                monthRuleInfoVO.setQuarterAmount(monthRuleFeeScale.getAmount());
                monthRuleInfoVO.setQuarterDays(monthRuleFeeScale.getDays());
            }
            if(monthRuleFeeScale.getMonthRentType().intValue() == MonthRentTypeEnum.YEAR.getCode()){
                monthRuleInfoVO.setYearAmount(monthRuleFeeScale.getAmount());
                monthRuleInfoVO.setYearDays(monthRuleFeeScale.getDays());
            }
        }

        return RestUtil.success(monthRuleInfoVO);
    }

    @ApiOperation(value = "开启/关闭规则")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType = "query", name = "id", value = "规则id", required = true),
            @ApiImplicitParam(paramType = "query", name = "status", value = "规则状态（OPEN：开启，CLOSE：关闭）", required = true),
    })
    @PostMapping(value = "switchStatus")
    public Rest switchStatus(@NotNull Long id, @NotNull RuleStatusEnum status) {
        MonthRule monthRule = new MonthRule();
        monthRule.setId(id);
        monthRule.setStatus(status.getCode());
        monthRuleService.updateById(monthRule);
        return RestUtil.success();
    }

    @ApiOperation(value = "查询咪网月租车规则")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping(value = "getMWMonthRule")
    public Rest getMWMonthRule(@RequestBody MWMonthRuleListForm form) {
        IPage<ParkInfoDTO> mwMonthRule = monthRuleService.getMWMonthRule(form);
        PageInfo<MWMonthRuleVO> mwMonthRuleVOPageInfo = JsonUtil.pageTransform(mwMonthRule, MWMonthRuleVO.class);
        return RestUtil.success(mwMonthRuleVOPageInfo);
    }

    @ApiOperation(value = "模糊查询月租规则名称")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping(value = "getMonthName")
    public Rest getMonthName(@RequestBody MonthRuleNameForm form) {
        IPage<MonthRuleDTO> monthRuleDTOIPage = monthRuleService.getMonthName(form);
        PageInfo<MonthRuleNameVO> monthRuleNamePage = JsonUtil.pageTransform(monthRuleDTOIPage, MonthRuleNameVO.class);
        return RestUtil.success(monthRuleNamePage);
    }




}

